package com.qingxin.shebei.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.service.FarmParkService;
import com.qingxin.shebei.entity.Shebei;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;

/**
 * 设备管理
 * @author Administrator
 *
 */
@Service
public class ShebeiService extends BaseService<Shebei, String> {
	
	@Autowired
	private FarmParkService farmParkService;
	
	/**
	 * 获取列表数据
	 * @param parkid
	 * @param name
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String parkid, String name, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.shebei.entity.Shebei(" +
				"s.sheid, s.parkid, s.name, s.address, s.ip, s.port, s.alleyway, s.username, s.password, s.shetype, s.brand, s.model,s.remark,s.createBy, s.createTime)" +
				" from Shebei s where s.parkid=:parkid ");
		querier.setParam("parkid", parkid);
		
		if(StringUtils.isNotEmpty(name)){
			name = name.replace("'", "");
			querier.append(" and s.name like :name ");
			querier.setParam("name", "%"+name+"%");
		}
		
		querier.append(pageParam.getOrderby(" order by s.name asc"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
		
	}
	
	
	/**
	 * 添加设备信息
	 * @param shebei
	 * @param parkid
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(Shebei shebei, String parkid, LoginUser loginUser, HttpServletRequest request){
		FarmPark park = farmParkService.get(parkid);
		if(park == null){
			return new SysError("参数错误");
		}

		shebei.setParkid(parkid);

		Date now = new Date();
		shebei.setCreateTime(now);
		shebei.setCreateBy(loginUser.getLogin_name());
		
		this.save(shebei);
		
		return null;
		
	}
	
	
	/**
	 * 修改设备信息
	 * @param shebei
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(Shebei shebei, LoginUser loginUser, HttpServletRequest request){
		Shebei old = getSafeEntity(shebei.getSheid());
		if(old == null){
			return new SysError("参数错误");
		}
		
		String fields = "name,address,ip,port,alleyway,username,password,shetype,brand,model,remark,createBy,createTime";
		BeanUtil.setTargetFields(old, shebei, fields, true);
		
		Updater<Shebei> updater = new Updater<>(old, fields);
		this.update(updater);
		
		return null;
	}
	
	
	/**
	 * 删除设备信息
	 * @param sheids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] sheids, LoginUser loginUser, HttpServletRequest request){
		for(String sheid : sheids){
			Shebei shebei = get(sheid);
			if(shebei==null){
				continue;
			}
			
//			Object obj = getByHql("select blockid from ParkBlock where parkBlock.sheid=?", sheid);
//			if(obj!=null){
//				return new SysError("设备 “"+shebei.getName()+"已绑定使用，不能删除.");
//			}
			
			this.delete(shebei);
		}
		return null;
	}
	
}
